/*
 * This file is a part of the RMI Plugin for Eclipse tutorials.
 * Copyright (C) 2002-7 Genady Beryozkin
 */
package storage;

/**
 *@author Adam,Aqeel
 */

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

import javax.media.MediaLocator;


import oldVersion.storage.RTPServer;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v2_2;



public class RMIServerImpl extends UnicastRemoteObject implements RMIServer {

	int port = 0;
	private String name;
	private static final long serialVersionUID = 5885886202424414094L;
	RTPServer rtps = new RTPServer();
	public String path = "src/mp3s/";
	RTPTransmit at;


	public RMIServerImpl(String name) throws RemoteException {
		this.name = name;
	}


	public int getPort() throws RemoteException{
		return port;
	}

	public Vector getSongs() throws RemoteException{
		Vector songs = new Vector(); 
		try{ 
			File dir = new File("src/mp3s/");
			String[] children = dir.list();
			if (children == null) {
				//do nothing
			} else {
				InetAddress thisIp =InetAddress.getLocalHost();
				System.out.println("IP:"+thisIp.getHostAddress());

				for (int i=0; i<children.length; i++) {					
					// Get filename of file or directory
					String filename = children[i];
					System.out.println(filename);
					if (filename.equals(".svn") || filename.startsWith("tu")){
						//do nothing
					}
					else{
						File sourcefile = new File("src/mp3s/" + filename);
						MP3File mp3file = new MP3File(sourcefile);
						ID3v2_2 t = (ID3v2_2) mp3file.getID3v2Tag();
						String artist = t.getLeadArtist();
						String title = t.getSongTitle();
						String album = t.getAlbumTitle();
						String location = thisIp.getHostAddress();
						SongObject so = new SongObject(artist,album,title,location,filename);
						System.out.println(so.toString());
						songs.addElement(so);
					}
				}
			}
		} catch (IOException e) {
			System.out.println("problem io in xml making");
			e.printStackTrace();
		} catch (TagException e) {
			System.out.println("problem tagging in xml making");
			e.printStackTrace();
		}
		return songs;
	}

	public void stream(String myHifiAddress){
		rtps.SetHifiAddress(myHifiAddress);
		rtps.run();
	}

	public String[] getMusicList(){

		path = "src/mp3s/"; 	//set directory path to share music with server
		File fi = new File(path);
		String songList[]= fi.list();	//now we have a string array contains list of songs

		
		return songList;

	}
	//transmit Music track chosen by server
	public double transmitMusicTrack(String trackName, String DestinationIP, String DestinationPORT){

		String url = "file:"+path+trackName;
		at = new RTPTransmit(new MediaLocator(url), DestinationIP , DestinationPORT,null);
		double result = at.start();
		System.out.println(result);
//		if (result != null) { //if returns null means transmitter created
//			System.err.println("Error : " + result);
//		}
		//at.stop(); //to stop sending stream
		return result;
	}
	
	public void stopTransmit(){
		
		at.stop();
		
	}

	public void begin() {
		try {
			Registry registry = LocateRegistry.getRegistry();
			registry.rebind(name, new RMIServerImpl(name));
//			registry.rebind("name", new RMIServerImpl("name"));
			port = registry.REGISTRY_PORT;
			System.out.println("started RMI server");
		} catch (RemoteException e) {
			System.err.println("Something happened trying to start RMI");
			e.printStackTrace();
			System.exit(-1);
		}
		System.out.println("The RMI server " + name +" is ready");
//		System.out.println("The RMI server is ready");
	}
}
